﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Automapping;
using FluentNHibernate;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using System.Reflection;
using System.Diagnostics;

namespace ToDoIt2.DB {

	public class myPrimaryKeyConvention : IIdConvention {
		public void Apply(IIdentityInstance i) {
			i.Column(i.EntityType.Name + "ID");
		}
	}

	public class myForeignKeyConvention : ForeignKeyConvention {
		protected override string GetKeyName(Member m, Type t) {
			if (m == null)
				return t.Name + "ID";
			return m.Name + "ID";
		}
	}

	class AutoMapping : DefaultAutomappingConfiguration {
		public override bool ShouldMap(Type type) {
			var ShouldMap = base.ShouldMap(type) && (type.Name.IndexOf("ObservableList") != -1 || type.Namespace.IndexOf("Persistent") != -1);
//			if(ShouldMap)
//				Debug.WriteLine(String.Format("ShouldMapType({0}) = {1}", type, ShouldMap));
			return ShouldMap;
		}
		public override bool ShouldMap(Member member) {
			if (member.Name == "RowUpdatePossible") {
				//	System.Diagnostics.Debugger.Break();
			}
			var ShouldMap = base.ShouldMap(member);
//			if(ShouldMap)
//			    Debug.WriteLine(String.Format("ShouldMapMember({0}::{1}) = {2}", member.DeclaringType.Name, member.Name, ShouldMap));
		    return ShouldMap;
		}
		public override bool IsId(Member member) {
		//	Debug.WriteLine(String.Format("{0}::{1}", member.DeclaringType.Name, member.Name));
			if(member.Name.IndexOf("ID") != -1 && member.DeclaringType.Name.IndexOf(member.Name.Replace("ID", "")) != -1)
				return true;
			return false;
		}
	}
}